﻿using Inovout.Cloud.Applications.Security.Models;
using Inovout.Cloud.Web.Mvc;
using Inovout.Services;
using System.Web.Mvc;
using System.Linq;
using Inovout.Web;
using Inovout.Runtime;
using Inovout.Cloud.Applications.Security.Services;

namespace Inovout.Cloud.Storage.Console.Web.Controllers
{
    public class BucketController : CloudController
    {
        public override ActionResult Index()
        {
            var productConfiguration = ((WebApplication)ComponentRegistry.Resolve<IApplication>()).ProductConfiguration;
            ViewBag.RootUri = productConfiguration.Children[0].Children
                .Single(c => c.Attributes["name"].Equals("Inovout.Cloud.Storage.WebSite")).Attributes["url"];

            //ViewBag.RootUri = "http://" + this.Request.Url.Authority + "/";
            //IRepositoryService<Bucket> bucketService = Inovout.ComponentRegistry.Resolve<IRepositoryService<Bucket>>();
            //Bucket bucket = bucketService.FindAll(1,1).ToArray()[0];
            //ViewBag.Project = bucket.Project.Name;

            //IApplicationService applicationService = Inovout.ComponentRegistry.Resolve<IApplicationService>();
            //ViewBag.AppKey = applicationService.FindByOwner(bucket.CreatedUserName).ToArray()[0].Name;

            ApplicationUser applicationUser = ComponentRegistry.Resolve<IApplicationUserService>().FindByApplication((Inovout.Cloud.Applications.Models.Application)base.Project.Applications.ToArray()[0]);
            ViewBag.Project = Project.Name;
            ViewBag.AppKey = applicationUser.PublicApiAccessCredentials.OfType<BrowserApplicationCredential>().ToArray()[0].ApiKey;


            return base.Index();
        }
    }
}